package demo.set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class RedisDemo {

    public static void test1(Jedis jedis){
        System.out.println("add 和 smembers");
        jedis.flushAll();

        jedis.sadd("key", "11", "22", "33", "44");

        Set<String> result = jedis.smembers("key");
        System.out.println("result = " + result);
    }


    public static void test2(Jedis jedis){
        System.out.println("sismembers");
        jedis.flushAll();

        jedis.sadd("key", "11", "22", "33", "44");

        boolean result = jedis.sismember("key", "1111");
        System.out.println("result = " + result);
    }


    public static void test3(Jedis jedis){
        System.out.println("scard");
        jedis.flushAll();

        jedis.sadd("key", "11", "22", "33", "44");

        long result = jedis.scard("key");
        System.out.println("result = " + result);
    }


    public static void test4(Jedis jedis){
        System.out.println("spop");
        jedis.flushAll();

        jedis.sadd("key", "11", "22", "33", "44");

        String result = jedis.spop("key");
        System.out.println("result = " + result);

        result = jedis.spop("key");
        System.out.println("result = " + result);
    }


    public static void test5(Jedis jedis){
        System.out.println("sinter 和 sinterstore");
        jedis.flushAll();

        jedis.sadd("key1", "11", "22", "33");
        jedis.sadd("key2", "11", "22", "44");

        Set<String>  result = jedis.sinter("key1", "key2");
        System.out.println("result = " + result);

        long len = jedis.sinterstore("key3", "key1", "key2");
        System.out.println("len = " + len);

        result = jedis.smembers("key3");
        System.out.println("result = " + result);
    }


    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()){
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
            test5(jedis);
        }
    }
}
